<script language="javascript" type="text/javascript">

    var http_request = false;

	function loadDayList(groupID) {
        document.getElementById("status").innerHTML = "Loading data...";
		makeRequest("http://transpo.uconn.edu/includes/ajax.php?task=sched_maker_disp_days&group_id=" + groupID,alertContents);
	}

	function loadLineList(dayID) {
        document.getElementById("status").innerHTML = "Loading data...";
		makeRequest("http://transpo.uconn.edu/includes/ajax.php?task=sched_maker_disp_lines&table_id=" + dayID,alertContents);
	}
	
	function moveUp(table,where,clause,column,primary,subject) {
		urlToSubmit = "http://<?= $_SERVER['SERVER_NAME'] ?>/includes/ajax.php?task=moveItemUp&table=" + table + "&where=" + where + "&clause=" + clause + "&column=" + column + "&primary=" + primary + "&subject=" + subject;
		document.getElementById("status").innerHTML = "Saving...";
		makeRequest(urlToSubmit,alertContents);
	}
	function moveDown(table,where,clause,column,primary,subject) {
		urlToSubmit = "http://<?= $_SERVER['SERVER_NAME'] ?>/includes/ajax.php?task=moveItemDown&table=" + table + "&where=" + where + "&clause=" + clause + "&column=" + column + "&primary=" + primary + "&subject=" + subject;
		document.getElementById("status").innerHTML = "Saving...";
		makeRequest(urlToSubmit,alertContents);
	}

    function makeRequest(url,toWhom) {
        http_request = false;
        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest(); 
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/plain');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }

		http_request.abort();
		http_request.onreadystatechange = toWhom;
        http_request.open('GET', url, true);
        http_request.send(null);
    }

    function alertContents() {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                document.getElementById("output").innerHTML = http_request.responseText;
                document.getElementById("status").innerHTML = "";
            } else {
                alert('There was a problem with the request.');
            }
        }
    }
	
    function moveResults() {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                alert("Your move was successful! - reloading data");
            } else {
                alert('There was a problem with the move request.');
            }
        }
    }

</script>
<?php

	//while editing system..
	if(!true){
		if(!checkQualified(1000)) {
			errorText("Sorry, you must be an AM in order to edit schedules.");
			return;
		}
	}

	//check to see if we've already connected to the mysql db..
	if(!is_resource($link)) {
		  require_once("/export/www/transportation/includes/variables.php");
			//require(SESSION_HANDLER_FILE);
			//session_start();
		  //session_start();
		  define(LOGGEDIN,false);
	}


	switch($_REQUEST['level']) {
		case '':
?>

	<div style="background-color:#D2D2FF; border: 1px solid black; text-align:center; padding: 5px; margin: 20px;">
		<form name="form1" method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
		<input type="hidden" name="sched_maker" value="1">
		Pick a group to edit: 
		<select name="group">
			<option value="0">- Create New Group -</option>
			<?php
				$sql = sprintf("SELECT * FROM `t_sched_groups` WHERE `status` > 0");
				if(DEBUG) print $sql;
				$result = mysql_query($sql);
				while($row = mysql_fetch_assoc($result)) {
			?>			
			<option value="<?= $row['group_id'] ?>"><?= $row['description'] ?></option>
			<?php
				}
				mysql_free_result($result);
			?>
		</select>
		<input type="hidden" name="level" value="group">
		<input type="submit" name="Submit" value="Go --&gt;"> 
		</form>
	</div>
<?php
			break;
		case 'group':
		$display_group = $_REQUEST['group'];
	if( isset($_REQUEST['edit_group']) ) {
		//we have data to save
		print "Saving data...";
		//are we a new group? or should we just update an old group?
		if($_REQUEST['edit_group'] == 0) {
			$sql = sprintf("INSERT INTO `t_sched_groups` (`description`,`status`) VALUES (%s,%s)",quote_smart($_REQUEST['description']),quote_smart($_REQUEST['status']));
		} else {
			$sql = sprintf("UPDATE `t_sched_groups` SET `description` = %s , `status` = %s WHERE `group_id` = %s",quote_smart($_REQUEST['description']),quote_smart($_REQUEST['status']),quote_smart($_REQUEST['edit_group']));
		}
		if(DEBUG) print $sql;
		$result = mysql_query($sql);
		if(mysql_affected_rows() == 1) {
			alertText("Group Data Saved!");
			if($_REQUEST['edit_group'] == 0)
				$display_group = mysql_insert_id();
		}
		
		//check for rule file upload
		if($_FILES['rulefile']['size'] > 0) {
			//lets save the new file
			$upload_file = RULES_FOLDER . $display_group . '.htm';
			if(DEBUG) print "saving new rules file to " . $upload_file;
			if(move_uploaded_file($_FILES['rulefile']['tmp_name'], $upload_file)) {
				alertText("New Rule File Uploaded!");
			} else {
				errorText("An error occurred while uploading the new rule file!");
			}
		}
	}
	if( $display_group != 0 ) {
		$sql = sprintf("SELECT * FROM `t_sched_groups` WHERE `group_id` = %s",quote_smart($display_group));
		if(DEBUG) print $sql;
		$result = mysql_query($sql);
		if(mysql_num_rows($result) != 1) {
			errorText("Invalid Group ID!");
		} else {
			$groupData = mysql_fetch_assoc($result);
		}
	}
		
?>
<div style="text-align:center; ">
<p><a href="<?= $_SERVER['PHP_SELF'] ?>">Return to Pick a Group</a></p>
<form name="form2" method="post" enctype="multipart/form-data" action="<?= $_SERVER['PHP_SELF'] ?>">
<input type="hidden" name="sched_maker" value="1">
<table border="0" style="margin:auto; ">
  <tr>
    <td style="text-align:left; ">Group ID </td>
    <td style="text-align:left; "><?= $display_group ?><input type="hidden" name="edit_group" value="<?= $display_group ?>"><input type="hidden" name="group" value="<?= $display_group ?>"><input type="hidden" name="level" value="group"></td>
  </tr>
  <tr>
    <td style="text-align:left; ">Group Title </td>
    <td style="text-align:left; "><input type="text" name="description" value="<?= $groupData['description']?>"></td>
  </tr>
  <tr>
    <td style="text-align:left; ">View Rule File </td>
    <td style="text-align:left; "><?php if($display_group == 0) { ?>No rule file<?php } else { ?><a target="_blank" href="http://www.transpo.uconn.edu/rules/<?= $display_group ?>.htm">Click Here</a><?php } ?> </td>
  </tr>
  <tr>
    <td style="text-align:left; ">Upload New Rule File </td>
    <td style="text-align:left; "><input type="file" name="rulefile"></td>
  </tr>
  <tr>
    <td style="text-align:left; ">Group Status </td>
    <td style="text-align:left; "><select name="status">
      <option value="0" <?php if($groupData['status'] == 0) print 'selected'; ?>>Closed</option>
      <option value="1" <?php if($groupData['status'] == 1) print 'selected'; ?>>Pending</option>
      <option value="2" <?php if($groupData['status'] == 2) print 'selected'; ?>>Signups</option>
    </select></td>
  </tr>
  <tr>
    <td style="text-align:left; ">Recurring</td>
    <td style="text-align:left; "><input name="radiobutton" type="radio" value="radiobutton" disabled>
      Yes 
      <input name="radiobutton" type="radio" value="radiobutton" disabled> 
      No </td>
  </tr>
  <tr>
    <td style="text-align:left; ">Point Value </td>
    <td style="text-align:left; "><input type="text" name="textfield" disabled></td>
  </tr>
  <tr>
    <td colspan="2" style="text-align:center; "><input type="submit" name="Submit" value="Save"></td>
  </tr>
</table>
</form>
</div>
<div align="center" style="text-align:center; font-size: 14px; font-weight: bold; height: 20px;" id="status"></div>
<div style="text-align:center;" id="output"></div>
<script language="javascript" type="text/javascript">
	loadDayList(<?= $display_group ?>);
</script>

<?php

			break;
		case 'day':
		$display_day = $_REQUEST['day'];
	if( isset($_REQUEST['edit_day']) ) {
		//we have data to save
		print "Saving data...";
		//are we a new group? or should we just update an old group?
		if($_REQUEST['edit_day'] == 0) {
//-->			$sql = sprintf("INSERT INTO `t_sched_groups` (`description`,`status`) VALUES (%s,%s)",quote_smart($_REQUEST['description']),quote_smart($_REQUEST['status']));
		} else {
//-->			$sql = sprintf("UPDATE `t_sched_groups` SET `description` = %s , `status` = %s WHERE `group_id` = %s",quote_smart($_REQUEST['description']),quote_smart($_REQUEST['status']),quote_smart($_REQUEST['edit_group']));
		}
		if(DEBUG) print $sql;
		$result = mysql_query($sql);
		if(mysql_affected_rows() == 1) {
			alertText("Day Data Saved!");
			if($_REQUEST['edit_day'] == 0)
				$display_day = mysql_insert_id();
		}
		
	}
	if( $display_day != 0 ) {
		$sql = sprintf("SELECT * FROM `t_sched_main` WHERE `table_id` = %s",quote_smart($display_day));
		if(DEBUG) print $sql;
		$result = mysql_query($sql);
		if(mysql_num_rows($result) != 1) {
			errorText("Invalid day ID!");
		} else {
			$dayData = mysql_fetch_assoc($result);
		}
	}
?>
<div style="text-align:center; ">
<p><a href="<?= $_SERVER['PHP_SELF'] ?>">Return to Pick a Day</a></p>
<table border="1" style="margin:auto; ">
  <tr>
    <td style="text-align:left; ">Day ID: </td>
    <td style="text-align:left; "><?= $display_day ?><input type="hidden" name="edit_day" value="<?= $display_day ?>"></td>
  </tr>
  <tr>
    <td style="text-align:left; ">Member of: </td>
    <td style="text-align:left; ">		<select name="group">
			<?php
				$sql = sprintf("SELECT * FROM `t_sched_groups` WHERE `status` > 0");
				if(DEBUG) print $sql;
				$result = mysql_query($sql);
				while($row = mysql_fetch_assoc($result)) {
			?>			
			<option value="<?= $row['group_id'] ?>" <?php if($row['group_id'] == $dayData['group_id']) print "selected"; ?>><?= $row['description'] ?></option>
			<?php
				}
				mysql_free_result($result);
			?>
		</select>
</td>
  </tr>
  <tr>
    <td style="text-align:left; ">Day Description: </td>
    <td style="text-align:left; "><input type="text" name="description" value="<?= $dayData['description'] ?>"></td>
  </tr>
  <tr>
    <td style="text-align:left; ">Day Order: </td>
    <td style="text-align:left; "><span id="dayOrder">Auto (<?= $dayData['order']?>)</span><?= displayIcon(ICON_DOWN) ?> <?= displayIcon(ICON_UP) ?></td>
  </tr>
  <tr>
    <td style="text-align:left;">Week:</td>
    <td style="text-align:left;">
    <input type="text" name="week" value="<?= $dayData['week']?>"></td>
  </tr>
  <tr>
    <td colspan="2" style="text-align:center; "><input type="submit" name="Submit" value="Save"></td>
    </tr>
</table>
</div>
<div align="center" style="text-align:center; font-size: 14px; font-weight: bold; height: 20px;" id="status"></div>
<div style="text-align:center;" id="output"></div>
<script language="javascript" type="text/javascript">
	loadLineList(<?= $display_day ?>);
</script>
<?php			
			break;
	}
	if (DEBUG) print_r($_FILES);
?>
